//
//  AppDelegate.m
//  HDesign
//
//  Created by admin on 4/19/14.
//  Copyright (c) 2014 admin. All rights reserved.
//

#import "AppDelegate.h"
#import "HTempViewController.h"

NSString * const ADD_BUTTON_IMAGE = @"add.png";


@implementation AppDelegate

static int _viewControllerId = 0;

#pragma mark - LIExposeControllerDelegate Methods

- (BOOL)canAddViewControllersForExposeController:(LIExposeController *)exposeController {
    return YES;
}

- (BOOL)exposeController:(LIExposeController *)exposeController canDeleteViewController:(UIViewController *)viewController {
    return YES;
}

#pragma mark - LIExposeControllerDataSource Methods

- (UIView *)backgroundViewForExposeController:(LIExposeController *)exposeController {
    UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0,
                                                          0,
                                                          exposeController.view.frame.size.width,
                                                          exposeController.view.frame.size.height)];
    v.backgroundColor = [UIColor darkGrayColor];
    return v;
}

- (void)shouldAddViewControllerForExposeController:(LIExposeController *)exposeController {
    [exposeController addNewViewController:[self newViewControllerForExposeController:exposeController]
                                  animated:YES];
}

- (UIView *)addViewForExposeController:(LIExposeController *)exposeController {
    UIView *addView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:ADD_BUTTON_IMAGE]];
    return addView;
}

- (UIView *)exposeController:(LIExposeController *)exposeController overlayViewForViewController:(UIViewController *)viewController {
    if ([viewController isKindOfClass:[UINavigationController class]]) {
        viewController = [(UINavigationController *)viewController topViewController];
    }
    if ([viewController isKindOfClass:[HTempViewController class]]) {
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0,
                                                                    0,
                                                                    viewController.view.bounds.size.width,
                                                                    viewController.view.bounds.size.height)];
        label.backgroundColor = [UIColor colorWithWhite:0 alpha:0.4];
        label.text = viewController.title;
        label.textColor = [UIColor whiteColor];
        label.textAlignment = NSTextAlignmentCenter;
        label.font = [UIFont boldSystemFontOfSize:48];
        label.adjustsFontSizeToFitWidth = YES;
        label.shadowColor = [UIColor blackColor];
        label.shadowOffset = CGSizeMake(1, 1);
        return label;
    } else {
        return nil;
    }
}
#pragma mark 页面下部标题
- (UILabel *)exposeController:(LIExposeController *)exposeController labelForViewController:(UIViewController *)viewController {
    if ([viewController isKindOfClass:[UINavigationController class]]) {
        viewController = [(UINavigationController *)viewController topViewController];
    }
    if ([viewController isKindOfClass:[HTempViewController class]]) {
        UILabel *label = [[UILabel alloc] init];
        label.backgroundColor = [UIColor clearColor];
        label.text = viewController.title;
        label.textColor = [UIColor whiteColor];
        label.textAlignment = NSTextAlignmentCenter;
        label.font = [UIFont boldSystemFontOfSize:14];
        label.shadowColor = [UIColor blackColor];
        label.shadowOffset = CGSizeMake(1, 1);
        [label sizeToFit];
        CGRect frame = label.frame;
        frame.origin.y = 4;
        label.frame = frame;
        return label;
    } else {
        return nil;
    }
}

/**
 Optional Header View
 */
//- (UIView *)headerViewForExposeController:(LIExposeController *)exposeController {
//    UINavigationBar *headerBar = [[[UINavigationBar alloc] initWithFrame:CGRectMake(0,
//                                                                                    0,
//                                                                                    exposeController.view.frame.size.width,
//                                                                                    44)] autorelease];
//    UILabel *titleView = [[[UILabel alloc] init] autorelease];
//    titleView.backgroundColor = [UIColor clearColor];
//    titleView.text = NSLocalizedString(@"expose_title", @"expose_title");
//    titleView.textColor = [UIColor whiteColor];
//    titleView.shadowColor = [UIColor darkGrayColor];
//    titleView.shadowOffset = CGSizeMake(0, -0.5);
//    titleView.userInteractionEnabled = YES;
//    titleView.font = [UIFont boldSystemFontOfSize:20];
//    [titleView sizeToFit];
//    UITapGestureRecognizer *exposeGesture = [[[UITapGestureRecognizer alloc] initWithTarget:exposeController action:@selector(toggleExpose)] autorelease];
//    [titleView addGestureRecognizer:exposeGesture];
//    UINavigationItem *navItem = [[[UINavigationItem alloc] init] autorelease];
//    navItem.titleView = titleView;
//    headerBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
//    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
//        headerBar.autoresizingMask |= UIViewAutoresizingFlexibleHeight;
//    }
//    headerBar.items = [NSArray arrayWithObject:navItem];
//    return headerBar;
//}

/**
 Optional Footer View
 */
//- (UIView *)footerViewForExposeController:(LIExposeController *)exposeController {
//    UIToolbar *toolBar = [[[UIToolbar alloc] initWithFrame:CGRectMake(0,
//                                                                      0,
//                                                                      exposeController.view.frame.size.width,
//                                                                      44)] autorelease];
//    toolBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
//    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
//        toolBar.autoresizingMask |= UIViewAutoresizingFlexibleHeight;
//    }
//    toolBar.items = [NSArray arrayWithObject:exposeController.editButtonItem];
//    return toolBar;
//}

#pragma mark - Helper Methods

- (UIViewController *)newViewControllerForExposeController:(LIExposeController *)exposeController {
    UIViewController *vc = [[HTempViewController alloc] init];
    vc.title = [NSString stringWithFormat:NSLocalizedString(@"view_title_format_string", @"view_title_format_string"), _viewControllerId];
    _viewControllerId++;
    return [[UINavigationController alloc] initWithRootViewController:vc];
    //    return vc;
}

#pragma mark - UIApplicationDelegate Methods


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    LIExposeController *exposeController = [[LIExposeController alloc] init];
    exposeController.exposeDelegate = self;
    exposeController.exposeDataSource = self;
    exposeController.editing = YES;
    
    exposeController.viewControllers = [NSMutableArray arrayWithObjects:
                                        [self newViewControllerForExposeController:exposeController],
                                        nil];
    

    self.window.rootViewController = exposeController;
    [self.window makeKeyAndVisible];
    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application
{
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application
{
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
